<?php
/*
 * Created on Apr 26, 2012
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 if (isset($_POST['ok'])){
 	$u = NULL;
	$p = NULL;
	$msg = NULL;
 	//Client check
 	if ($_POST['username'] == NULL){
 		$msg = 'Please enter your username <br/>';
 	}else{
 		$u = $_POST['username'];
 	}
 	
 	if ($_POST['password'] == NULL){
 		$msg .= 'Please enter your password <br/>';
 	}else{
 		$p = $_POST['password'];
 	}
 	
 	//Server check
 	if ($u && $p){
 		$conn = mysql_connect('localhost','root','vertrigo') or die ('can not connect to server'); 		
 		mysql_select_db('iblog', $conn);
 		$sql = "select * from user where username='" . $u . "' and password='". $p ."'";
 		$query = mysql_query($sql);
 		if (mysql_num_rows($query)==0){
 			$msg = "Username and password is not correct, pls try again<br/>";
 		}else{
 			$msg = "Logged in<br/>";
 			$row = mysql_fetch_array($query);
 			session_start();
 			$_SESSION['userid'] = $row['username'];
 			$_SESSION['level'] = $row['level'];
 		} 
 		mysql_close($conn);		
 	}
 }
?>
<html>
	<head>
		<title>Login</title>
	</head>
	<body>
		<form action="login.php" method="post">
			<?php global $msg; echo $msg; ?>
			Username: <input type="text" name="username" size="25"/><br/>
			Password: <input type="password" name="password" size="25"/><br/>
			Captcha: <input type="text" name="captcha" size="25"/> <br/>
			<img src="captcha.php"/><br/>
			<input type="submit" name="ok" value="Login"/>
		</form>
	</body>
</html>